package com.tmc.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.baseinfo.Country;
import com.tmc.service.baseinfo.CountryServiceImpl;
import com.tmc.web.BaseController;

@Controller
@RequestMapping("/country")
public class CountryController extends BaseController {

	@Autowired
	private CountryServiceImpl service;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView findist(Country cond, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		if (pageIndex == null || pageIndex < 1)
			pageIndex = 1;
		if (pageSize == null || pageIndex < 1 || pageIndex > 1000)
			pageSize = 10;
		Map<String, Object> map = new HashMap<String, Object>();
		Pagination<Country> page = service.findByCon(context, cond, pageIndex, pageSize);
		map.put("page", page);
		map.put("cond", cond);
		map.put("context", context);
		return new ModelAndView("views/baseInfo/country-list", map);
	}
	
	@RequestMapping(value = "/editPage")
	public ModelAndView editPage(Long countryId, HttpServletRequest request) {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		Country country = null;
		if (countryId != null)
			country = service.getById(context, countryId);
		else
			country = new Country();
		map.put("country", country);
		map.put("context", context);
		return new ModelAndView("views/baseinfo/country-edit", map);
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save(Country country) {
		CommonResult cr = service.save(new ContextInfo(), country);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(Long countryId) throws Exception {
		CommonResult cr = service.delete(new ContextInfo(), countryId);
		return cr.toJson();
	}
	
	@RequestMapping(value = "/updatefromcity", method = RequestMethod.POST)
	public ModelAndView updatefromairport(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		service.updateFromCity(context);
		map.put("cond", new Country());
		map.put("page", new Pagination<Country>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/country-list", map);
	}
	
	@RequestMapping(value = "/updatefromcontinent", method = RequestMethod.POST)
	public ModelAndView updatefromcontinent(HttpServletRequest request) throws Exception {
		ContextInfo context = this.getContextInfo(request);
		Map<String, Object> map = new HashMap<String, Object>();
		service.updateFromContinent(context);
		map.put("cond", new Country());
		map.put("page", new Pagination<Country>());
		map.put("context", context);
		return new ModelAndView("views/baseInfo/country-list", map);
	}
	
}
